package com.example.prolyric.database;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;


public class SqliteServices extends SQLiteOpenHelper {
	
	
	private static final String DB_PATH="/data/data/com.example.prolyric/databases/";
	private static final String DB_NAME="prolyric.s3db";
	private static final String CON_STRING=DB_PATH+DB_NAME;
	
	SQLiteDatabase mydb=null;
	Context mycontext=null;
	
	
	public SqliteServices(Context context)
	{
		super(context, DB_NAME, null, 1);
	
		this.mycontext=context;
	}
	

//*************Xu li lien quan den database *****************//

	public boolean createDatabase() throws IOException
	{
		
		if(!checkDatabase())
		{
			copyDatabase();
			return true; 
		}
	
		return false;
	}


	public void openDatabase()
	{
		try
		{
			createDatabase();
			this.mydb=SQLiteDatabase.openDatabase(CON_STRING, null, SQLiteDatabase.OPEN_READWRITE);
		}
		
		catch(Exception ex)
		{
			Log.d("SQLITE", "Don't create database");
		}
				
	}
	
	public boolean checkDatabase()
	{
		SQLiteDatabase temp=null;
		try
		{
		 temp=SQLiteDatabase.openDatabase(CON_STRING, null, SQLiteDatabase.OPEN_READWRITE);
		}
		catch(Exception e)
		{
			
		}
		if(temp!=null)
			temp.close();	
		return temp!=null?true:false;
	}

public  void copyDatabase() throws IOException
{
	
	InputStream input = mycontext.getAssets().open(DB_NAME);
	
	OutputStream out = new  FileOutputStream(CON_STRING);
	
	byte[] buffer = new byte[1024];
	int length;
	
	while((length = input.read(buffer))>0){
			out.write(buffer,0,length);			
	}
	
	out.flush();
	out.close();
	input.close();
	
}
//**************************end ***************//

//*****************Thao tac du lieu ********************//

public void exeSQL(String sql){
	openDatabase();
	this.getWritableDatabase();
	
	this.mydb.execSQL(sql);
	
	this.mydb.close();
	
}

public Cursor queryData(String query)
{	
	openDatabase();
	this.getReadableDatabase();
	
	return this.mydb.rawQuery(query,null);	
	
}
public boolean deleteReminder(int rowId)
{
	
	return mydb.delete("Reminder", "id" + "=" + rowId, null) >0;
}
public boolean updateAlertRe(int rowId, String Status)
{
	ContentValues cv = new ContentValues();
	cv.put("Status", Status);

	return mydb.update("AlertReminder", cv, "id" +"="+ rowId, null)>0;
}
public int getIDAlertRe(String today)
{
	openDatabase();
	Cursor mCursor;
	int k = -1;
	mCursor = this.queryData("select * from AlertReminder");
	if(mCursor.moveToFirst())
	{
		do
		{
			if(mCursor.getString(1).equals(today))
			{
				k = Integer.parseInt(mCursor.getString(0).toString());
			}
		}
		while(mCursor.moveToNext());
	}
	return k;
	
}	
//**************Override these method*************************//
	@Override
	public synchronized void close() 
	{
		// TODO Auto-generated method stub
		if(this.mydb!=null)
			mydb.close();
		super.close();
	}
	@Override
	public void onCreate(SQLiteDatabase db) {
		// TODO Auto-generated method stub
		
	}


	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		// TODO Auto-generated method stub
		
	}
		
	
	
	
	

}
